import json
import os
from tls_deal import split_json_by_sn, merge_csv_files, sn_qujian
from chuangao_deal import get_gantary_id, save_gantary_data
from utils import get_all_files, get_direct_subdirectories


def deal_TLS_sim():
    # 原始卡口数据
    path = r'D:\GJ\项目\事故检测\模拟数据\kako_data20250326.log'
    # 按日期存储整天的json数据
    day = '2025-03-26'
    # 以日期形式存储json数据
    directory = r"D:\GJ\项目\事故检测\模拟数据\timestamp"
    # 处理数据中各类数据的保存目录
    csv_path = r"D:\GJ\项目\事故检测\模拟数据\output"

    if not os.path.exists(directory):
        os.makedirs(directory)
    save_path = os.path.join(directory, day+'.json')
    if not os.path.exists(csv_path):
        os.makedirs(csv_path)

    # 1.将log日志文件转为json格式
    with open(save_path, 'w', encoding='utf-8') as ff:
        with open(path, 'r', encoding='utf-8') as f:
            for line in f:
                data = json.loads(line)
                data = json.loads(data)
                if data['timestamp'][:7] == '2025-01':
                    continue
                data_ = json.dumps(data, ensure_ascii=False)
                ff.write(data_ + '\n')

    # 2.处理json数据，转为单门架的csv数据
    for file_path in get_all_files(directory, extension=".json"):
        daily_file_path = file_path
        split_json_by_sn(daily_file_path, csv_path)

    # 3.处理单门架的csv数据，合并为门架区间的csv数据，csv命名规则为（上游门架ID，下游门架ID-年月日）
    for file_path in get_direct_subdirectories(csv_path):
        print(file_path)
        for file1_path, file2_path in sn_qujian:
            merge_csv_files(file_path, day, file1_path, file2_path)
            print(file1_path, file2_path)


def deal_ChuanGao_data():
    file_path = r'D:\GJ\项目\事故检测\xlsx\达陕公司达陕段11.xlsx'
    data_folder = r"D:\GJ\项目\事故检测\川高事故门架数据\事故门架数据\达陕公司达陕段\牌识流水"
    save_path = r"D:\GJ\项目\事故检测\达陕公司达陕段"
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # 结合真值表获取门架区间组合
    Gid_dict = get_gantary_id(file_path, up_name="事故最近门架编号流水1", down_name="事故最近门架编号流水2")
    # 处理门架区间数据，保存为单区间csv文件
    save_gantary_data(data_folder, Gid_dict, save_path, up_name="事故最近门架编号流水2", down_name="事故最近门架编号流水1")


if __name__ == '__main__':
    # 将原始数据处理为门架区间的csv数据，csv命名规则为（上游门架ID，下游门架ID - 年月日），用作后续离线数据分析的输入文件

    # 预处理铜锣山模拟日志数据，保存为单区间csv文件
    deal_TLS_sim()

    # 预处理川高公司达陕段牌识流水数据，保存为单区间csv文件
    # deal_ChuanGao_data()


